package com.example.managemyself.api.config;

import com.example.managemyself.api.entity.User;
import com.example.managemyself.api.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 数据初始化器
 * 在应用启动时自动创建默认用户
 */
@Component
public class DataInitializer implements CommandLineRunner {
    
    private static final Logger logger = LoggerFactory.getLogger(DataInitializer.class);
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Override
    public void run(String... args) throws Exception {
        logger.info("开始初始化默认用户数据...");
        
        try {
            createDefaultUsers();
            logger.info("默认用户数据初始化完成");
        } catch (Exception e) {
            logger.error("默认用户数据初始化失败", e);
        }
    }
    
    private void createDefaultUsers() {
        // 创建默认管理员用户
        createDefaultAdmin();
        
        // 创建默认普通用户
        createDefaultUser();
    }
    
    private void createDefaultAdmin() {
        String username = "admin";
        
        // 检查管理员用户是否已存在
        if (userRepository.findByUsername(username).isPresent()) {
            logger.info("默认管理员用户已存在，跳过创建: {}", username);
            return;
        }
        
        try {
            User admin = new User();
            admin.setUsername(username);
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setEmail("admin@managemyself.com");
            admin.setRole(User.Role.ADMIN);
            admin.setStatus(User.Status.ACTIVE);
            admin.setFullName("系统管理员");
            
            User savedAdmin = userRepository.save(admin);
            logger.info("默认管理员用户创建成功: {} (ID: {})", savedAdmin.getUsername(), savedAdmin.getId());
            
        } catch (Exception e) {
            logger.error("创建默认管理员用户失败", e);
        }
    }
    
    private void createDefaultUser() {
        String username = "user";
        
        // 检查普通用户是否已存在
        if (userRepository.findByUsername(username).isPresent()) {
            logger.info("默认普通用户已存在，跳过创建: {}", username);
            return;
        }
        
        try {
            User user = new User();
            user.setUsername(username);
            user.setPassword(passwordEncoder.encode("user123"));
            user.setEmail("user@managemyself.com");
            user.setRole(User.Role.USER);
            user.setStatus(User.Status.ACTIVE);
            user.setFullName("普通用户");
            
            User savedUser = userRepository.save(user);
            logger.info("默认普通用户创建成功: {} (ID: {})", savedUser.getUsername(), savedUser.getId());
            
        } catch (Exception e) {
            logger.error("创建默认普通用户失败", e);
        }
    }
} 